package com.ccl.iot;import com.ccl.iot.IOTObject.IOTVariable;

public abstract class IOTFormula<TYPE> implements IOTVariable<TYPE> {
	private String iName = null;
	private TYPE iValue = null;
	
	IOTFormula(String aFormula){
		if (aFormula != null){
			String tParam[] = aFormula.split("=");
			
			if (tParam != null && tParam.length == 2){
				iName = new String(tParam[0].trim());
				iValue = resolveValue(tParam[1]);
			}
		}
	}
	
	IOTFormula(IOTFormula<?> aFormula){
		if (aFormula != null){
			Object tValue = aFormula.getValue();
			
			iName = aFormula.getName();
			
			if (tValue != null){
				iValue = resolveValue(tValue.toString());
			}
		}
	}
	
	protected abstract TYPE resolveValue(String aValue);
	
	@Override
	public long getID() {
		return this.hashCode();
	}
	
	public boolean isValid(){
		return iName != null && iValue != null;
	}
	
	public String getName(){
		return iName;
	}

	@Override
	public TYPE getValue() {
		return iValue;
	}

	@Override
	public boolean setValue(TYPE aValue) {
		if (aValue != iValue){
			iValue = aValue;
			
			return true;
		}
		
		return false;
	}

	@Override
	public String toString() {
		return iName + "=" + iValue;
	}

}
